package com.lzc.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzc.blog.admin.mapper.TagMapper;
import com.lzc.blog.admin.model.params.PageParam;
import com.lzc.blog.admin.pojo.Tag;
import com.lzc.blog.admin.service.TagService;
import com.lzc.blog.admin.vo.PageResult;
import com.lzc.blog.admin.vo.Result;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public Result tagList(PageParam pageParam) {
        IPage<Tag> page=new Page(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmptyOrWhitespaceOnly(pageParam.getQueryString())){
            // and name=?
            queryWrapper.like(Tag::getTagName,pageParam.getQueryString());
        }
        tagMapper.selectPage(page, queryWrapper);
        PageResult<Tag> pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setList(page.getRecords());
        return Result.success(pageResult);
    }

    @Override
    @Transactional
    public Result tagAdd(Tag tag) {
        int insert = tagMapper.insert(tag);
        System.out.println("tag=====>"+tag);
        if (insert!=0){
            return Result.success(insert);
        }

        return Result.fail(1001,"新增失败");


    }

    @Override
    @Transactional
    public Result tagDelete(int id) {
        int i = this.tagMapper.deleteById(id);
        if (i!=0){
            return Result.success(i);
        }
        return Result.fail(1002,"删除失败");
    }

    @Override
    public Result tagUpdate(Tag tag) {
        int i = this.tagMapper.updateById(tag);
        if (i!=0){
            return Result.success(i);
        }
        return Result.fail(1003,"修改失败");
    }
}
